import Foundation
import Moya

//微信QQ相关api
enum TencentNetApi {
    case wxToken(appid: String, secret: String, code: String, grant_type: String)//微信token
    case wxInfo(access_token: String, openid: String) //微信用户信息
    case qqInfo(access_token: String, openid: String, appId: String) //QQ用户信息
}

extension TencentNetApi: TargetType {
    
    var baseURL: URL {
        switch self {
        case .wxToken:
            return URL(string: "https://api.weixin.qq.com/sns/oauth2/access_token")!
        case .wxInfo:
            return URL(string: "https://api.weixin.qq.com/sns/userinfo")!
        case .qqInfo:
            return URL(string: "https://graph.qq.com/user/get_user_info")!
        }
    }
    
    var path: String {
        switch self {
        case .wxToken:
            return ""
        case .wxInfo:
            return ""
        case .qqInfo:
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .wxToken:
            return .get
        case .wxInfo:
            return .get
        case .qqInfo:
            return .get
        }
    }
    
    var task: Task {
        switch self {
        case .wxToken(let appid, let secret, let code, let grant_type):
            let dictQuery = ["appid": appid,"secret": secret,"code": code,"grant_type": grant_type]
            return .requestParameters(parameters: dictQuery, encoding: URLEncoding.default)
        case .wxInfo(let access_token, let openid):
            let dictQuery = ["access_token": access_token,"openid": openid]
            return .requestParameters(parameters: dictQuery, encoding: URLEncoding.default)
        case .qqInfo(let access_token, let openid, let appId):
            let dictQuery = ["access_token": access_token,"openid": openid,"oauth_consumer_key": appId]
            return .requestParameters(parameters: dictQuery, encoding: URLEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        let headers: [String: String] = [:]
        
        switch self {
        case .wxToken:
            return headers
        case .wxInfo:
            return headers
        case .qqInfo:
            return headers
        }
    }
    
    private var sampleData: Data? {
        // 通常用于模拟网络请求的响应数据
        return nil
    }
    
    var validate: Bool {
        return true  // 根据需要开启或关闭响应验证
    }
}
